open(BUFF,"test_graph");
#open(BUFF,"test");
open(WRBUFF,">out_annotate_antisense_final_2,3_sig_sd");
#$line=<BUFF>;
while($line=<BUFF>)
{
	chomp($line);
	$line =~ s/\s+/\t/g;
	$line =~ s/\t+/\t/g;
	$liney = $line;
        $liney =~ s/_boundry//g;
	#print $liney."\n";
	@array = split(/\t/,$liney);
        @exons = split('__',$array[1]);
	@bin = split('_',$array[0]);
	$bin_start = $bin[1];
	for (keys %hash)
    	{
        	delete $hash{$_};
    	}
        if(@exons > 1)
        {
=head
        	@ex=();
         	for($i=0;$i<@exons;$i++)
        	{
               		@ksk = (); 
			@ksk = split('_',$exons[$i]);
		        $ex[$i] = $ksk[0];
		
        	} 		
		@list_order = sort { $ex[$a] cmp $ex[$b] } 0 .. $#ex;
        	#print "lo  @list_order\n ex @ex\n";
        	#print "before @exons\n";
         	@ex1 = ();
        	for($i=0;$i<@exons;$i++)
        	{
	    		$ex1[$i] = $exons[$list_order[$i]];
        	}
         	@exons = @ex1;
         	#print "after @exons\n";
=cut
	$ex1[0] = $exons[0];
	@exons = ();
	@exons = @ex1
        }
	
	for($i=0;$i<@exons;$i++)
	{
		$hash{$i} = $exons[$i];
		#print @exons."\n";
	}
	$k ="k";
	@start = ();
	$start =0;
	@end = ();
	$end = 0;
	$result = "";
	#while(($key,$value) = each %hash)
	#{
	#	print "hash\t$key\t$value\n";
	#}
	foreach $key (sort keys %hash) 
	 { 
       		#print $key."\n";

     		@exn = split('_',$hash{$key});
                 #print $k."\t".$exn[0]."\n";
		if($exn[0] ne $k && $k ne "k")
		{
			#print "sucess\n";
			@start1 = ();
			@end1 = ();
			@list_start = ();
			@list_end = ();
			@sstart1 = ();
                        @send1 = ();
			for($i=0;$i<@start;$i++)
			{
				$start1[$i] = abs($start[$i] - $bin_start);
				$sstart1[$i] = $start[$i];
				$end1[$i] = abs($end[$i] - $bin_start);
				$send1[$i] = $end[$i];
			}
			@list_start = sort { $start1[$a] <=> $start1[$b] } 0 .. $#start1;
			@start1 = sort { $a <=> $b } @start1;
			@list_end = sort { $end1[$a] <=> $end1[$b] } 0 .. $#end1;
			@end1 =  sort { $a <=> $b } @end1; 
			$small_start = $start1[0];
			$small_end = $end1[0];
			if($small_start > $small_end)
			{
				$result = $result."\t$gene\t3'\t$small_end\t";
			}
			else
			{
				$result = $result."\t$gene\t5'\t$small_start";	
			}	
			@start = ();
			$start =0;
			@end = ();
			$end = 0;
		}
		if($hash{$key} =~ m/exon/)
		{
			$start[$start] = $exn[2];
			$start++;
			$end[$end] = $exn[3];
			$end++;
		}
		else
		{
			#print $hash{$key}."\n";
			$start[$start] = $exn[3]+1;
                        $start++;
                        $end[$end] = $exn[2]-1;
                        $end++;
			
		}
		$gene = $exn[0];
                #print "ss $line\t$gene\n";
		$k = $exn[0];
	}
	#print "@start\n@end\n";
	@start1 = ();
	@end1 = ();
	for($i=0;$i<@start;$i++)
	{
		#print "$bin_start\t$start[$i]\t$end[$i]\t$start1[$i]\t$end1[$i]\n";
		$start1[$i] = abs($start[$i] - $bin_start);
		$end1[$i] = abs($end[$i] - $bin_start);
		#print "$bin_start\t$start[$i]\t$end[$i]\t$start1[$i]\t$end1[$i]\n";
	}
	@start1 = sort { $a <=> $b } @start1;
        @end1 =  sort { $a <=> $b } @end1;
        $small_start = $start1[0];
      	$small_end = $end1[0];
	if($small_start > $small_end)
        {
		$result = $result."\t$gene\t3'\t$small_end";	
	}
        else
        {
        	$result = $result."\t$gene\t5'\t$small_start";
        }
	#for (keys %$hash)
    	#{
        #	delete $hash->{$_};
    	#}
	print  WRBUFF $line.$result."\n";
}
